package com.ysd.lis.service.qc.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabInstrSetup;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.qc.QcInstrSample;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.mapper.qc.QcInstrSampleMapper;
import com.ysd.lis.request.qc.QcInstrSampleDto;
import com.ysd.lis.request.qc.QcInstrSampleParam;
import com.ysd.lis.service.qc.QcInstrSampleService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-01-25
 */
@Service
@Slf4j
public class QcInstrSampleServiceImpl extends ServiceImpl<QcInstrSampleMapper, QcInstrSample> implements QcInstrSampleService {

    @Autowired
    QcInstrSampleMapper qcInstrSampleMapper;
    @Autowired
    LabItemDictMapper labItemDictMapper;

    @Override
    public Result findQcInstrSampleAndLabItemDictList(QcInstrSampleParam param) {
        MPJLambdaWrapper<QcInstrSample> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QcInstrSample.class);
        if (StringUtils.isNotBlank(param.getInstrId())) {
            queryWrapper.eq(QcInstrSample::getInstrId, param.getInstrId());
        }
        queryWrapper.eq(QcInstrSample::getDelFlag, 0)
                .orderByAsc(QcInstrSample::getCreateTime);
        List<QcInstrSample> sampleList = qcInstrSampleMapper.selectList(queryWrapper);


        MPJLambdaWrapper<LabItemDict> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabItemDict.class)
                .leftJoin(LabInstrSetup.class, "lis", LabInstrSetup::getRptGroup, LabItemDict::getRptGroup)
                .eq(LabItemDict::getDelFlag, 0)
                .eq(LabInstrSetup::getId, param.getInstrId());
        if(ToolsUtils.isNotEmpty(param.getPoct())){
            lambdaQueryWrapper.eq(LabItemDict::getPoct, param.getPoct());
        }
        List<LabItemDict> labItemDicts = labItemDictMapper.selectList(lambdaQueryWrapper);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("sampleList", sampleList);
        returnMap.put("labItemDicts", labItemDicts);
        return Result.succ(1, "查询成功", returnMap);
    }

    @Override
    public Result saveOrUpdateInstrSample(QcInstrSampleDto dto) {
        List<QcInstrSample> qcInstrSampleList = dto.getQcInstrSampleList();
        boolean b = this.saveOrUpdateBatch(qcInstrSampleList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteInstrSampleList(QcInstrSampleDto dto) {
        List<QcInstrSample> qcInstrSampleList = dto.getQcInstrSampleList();
        List<String> ids = qcInstrSampleList.stream().map(QcInstrSample::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
